package com.whq.thrift.api.common.thriftclient;

import java.io.Serializable;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingSocket;
import org.apache.thrift.transport.TNonblockingTransport;
import org.apache.thrift.transport.TSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ThriftServiceRecord implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ThriftServiceRecord.class);
	
	// Milliseconds timeout
	private static final int CONNECT_TIMEOUT_MS = 5000;
	
	private String ip = "";
	private int port = 0;
	
//	private boolean needRemove = false;
	
	private long lastCheckTimestamp = 0l;

	private boolean valid = false;
	
	private TNonblockingTransport nonblockingTransport = null;
	
	private TFramedTransport framedTransport = null;
	
	public void initNonblockingTransport() {
		try {
			nonblockingTransport = new TNonblockingSocket(ip, port, CONNECT_TIMEOUT_MS);
		} catch (Exception e) {
			LOGGER.error("new TNonblockingSocket(" + ip + ", " + port + ", " + CONNECT_TIMEOUT_MS + ") exception: ", e);
		} 
	}
	
	public void initFramedTransport() {
		try {
			framedTransport = new TFramedTransport(new TSocket(ip, port, CONNECT_TIMEOUT_MS));
		} catch (Exception e) {
			LOGGER.error("new TFramedTransport(" + ip + ", " + port + ", " + CONNECT_TIMEOUT_MS + ") exception: ", e);
		} 
	}
	
	public boolean isValid() {
		return valid;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}
	
//	public boolean isNeedRemove() {
//		return needRemove;
//	}
//
//	public void setNeedRemove(boolean needRemove) {
//		this.needRemove = needRemove;
//	}
	
	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public long getLastCheckTimestamp() {
		return lastCheckTimestamp;
	}

	public void setLastCheckTimestamp(long lastCheckTimestamp) {
		this.lastCheckTimestamp = lastCheckTimestamp;
	}
	
	public TNonblockingTransport getNonblockingTransport() {
		return nonblockingTransport;
	}

	public void setNonblockingTransport(TNonblockingTransport nonblockingTransport) {
		this.nonblockingTransport = nonblockingTransport;
	}
	
	public TFramedTransport getFramedTransport() {
		return framedTransport;
	}

	public void setFramedTransport(TFramedTransport framedTransport) {
		this.framedTransport = framedTransport;
	}

	public String toString() {
		return new ToStringBuilder(this).append("ip", this.getIp()).append("port", this.getPort()).toString();
	}

	public boolean equals(Object other) {
		if ((this == other)) {
			return true;
		}

		if (this.getIp() == null) {
			return false;
		}

		if (!(other instanceof ThriftServiceRecord)) {
			return false;
		}

		ThriftServiceRecord otherObj = (ThriftServiceRecord) other;
		return new EqualsBuilder().append(this.getIp(), otherObj.getIp()).append(this.getPort(), otherObj.getPort()).isEquals();
	}

	public int hashCode() {
		return new HashCodeBuilder().append(this.getIp()).append(this.getIp()).toHashCode();
	}
}
